package com.icesoft.xsnow.common.security.oauth.component;

import cn.hutool.core.map.MapUtil;
import com.icesoft.xsnow.common.core.constant.SecurityConstants;
import com.icesoft.xsnow.common.security.dto.XSnowUser;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @program: xsnow
 * @description: 根据checktoken的结果转化用户信息
 * @author: xuefeng.gao
 * @create: 2019-05-14 14:14
 **/
@Slf4j
public class XsnowUserAuthenticationConvert implements UserAuthenticationConverter {

    @Override
    public Map<String, ?> convertUserAuthentication(Authentication userAuthentication) {
        Map<String, Object> response = new LinkedHashMap<>();
        response.put(USERNAME, userAuthentication.getName());
        if (userAuthentication.getAuthorities() != null && !userAuthentication.getAuthorities().isEmpty()) {
            response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(userAuthentication.getAuthorities()));
        }
        return response;
    }

    @Override
    @SneakyThrows
    public Authentication extractAuthentication(Map<String, ?> map) {
        if (map.containsKey(SecurityConstants.XSNOW_USER)) {
            Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
            Map<String,Object> userinfo = (Map) map.get(SecurityConstants.XSNOW_USER);
            String username = MapUtil.getStr(userinfo,SecurityConstants.DETAILS_USERNAME);
            Integer userId = MapUtil.getInt(userinfo,SecurityConstants.DETAILS_USER_ID);
            String email = MapUtil.getStr(userinfo,SecurityConstants.DETAILS_EMAIL);
            String phone = MapUtil.getStr(userinfo,SecurityConstants.DETAILS_PHONE);
            String tenantId = MapUtil.getStr(userinfo,SecurityConstants.DETAILS_TENANT_ID);
            XSnowUser xSnowUser = new XSnowUser(userId,username, SecurityConstants.N_A,tenantId,phone,email,authorities);
            return new UsernamePasswordAuthenticationToken(xSnowUser, SecurityConstants.N_A, authorities);
        }
        return null;
    }

    private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
        Object authorities = map.get(AUTHORITIES);
        if (authorities instanceof String) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
        }
        if (authorities instanceof Collection) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
                    .collectionToCommaDelimitedString((Collection<?>) authorities));
        }
        throw new IllegalArgumentException("Authorities must be either a String or a Collection");
    }

}
